<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-08-15 16:45:26
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2022-12-21 17:10:12
 * @FilePath: \grazlink20\uc_sys\src\views\Asset\Pasture\components\See.vue
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { onBeforeMount, PropType, reactive, ref } from 'vue'
import type { TableData } from '@/api/uc/org/types'
import { Descriptions } from '@/components/Descriptions'
// 获取字典
import { useCache } from '@/hooks/web/useCache'

import { spCostDetailedListApi, spCostSeeApi } from '@/api/cy/xSign'

const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<TableData>>,
    default: () => null
  },
  detailSchema: {
    type: Array as PropType<DescriptionsSchema[]>,
    default: () => []
  },
  id: {
    type: String,
    default: null
  }
})
console.log(props)
const data = ref<any>({})
onBeforeMount(async () => {
  const res: any = await spCostSeeApi({ id: props.id })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    data.value = res.data
    let aa = JSON.parse(res.data.select_json)
    console.log(aa.select_json)
    const Ming = await spCostDetailedListApi({
      id: props.id
    })
      .catch(() => {})
      .finally(() => {})
    if (Ming) {
      detailedData.value = Ming.data
      console.log(detailedData.value, Ming.data, '1111')
    }
    // 明细信息处理一下，加上d_id
    // data.value = await changeDId(res.data)
    // 明细信息处理一下，加上d_id
    // await methods.setValues(data.value)
    data.value.selected = res.data
    console.log(props.id, aa.selected, '11111')
  }
})

const detailedData = ref()

const d_columns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    width: 30
  },
  {
    field: 'asset_code',
    label: '资产编码',
    width: 200
  },
  {
    field: 'earcon_code',
    width: 200,
    label: '耳标编码'
  },
  {
    field: 'breed',
    label: '畜别',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed', cellValue)
    },
    width: 100,
    search: {
      show: true
    }
  },
  {
    field: 'breed_sub',
    label: '品种',
    width: 200,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed_sub', cellValue)
    }
  },
  {
    field: 'loan_state',
    label: '贷款状态',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('loan_order_state', cellValue)
    }
  }
])

const d_columnss = reactive<TableColumn[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    width: 30
  },
  // {
  //   field: 'asset_code',
  //   label: '资产编码',
  //   width: 200
  // },
  // {
  //   field: 'earcon_code',
  //   width: 200,
  //   label: '耳标编码'
  // },
  {
    field: 'type',
    label: '费用科目',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('fy_state', cellValue)
    },
    width: 100,
    search: {
      show: true
    }
  },
  {
    field: 'amount',
    label: '金额',
    width: 200
  },
  {
    field: 'note',
    width: 200,
    label: '备注'
  }
])
</script>

<template>
  <ContentWrap title="主数据">
    <Descriptions :schema="detailSchema" :data="currentRow || {}">
      <template #breed="{ row }: { row: any }">
        <div>{{ getDictLabel('breed', row.breed) }}</div>
      </template>
      <template #breed_sub="{ row }: { row: any }">
        <div>{{ getDictLabel('breed_sub', row.breed_sub) }}</div>
      </template>
      <template #type="{ row }: { row: any }">
        <div>{{ getDictLabel('zcfy_type', row.type) }}</div>
      </template>
      <template #state="{ row }: { row: any }">
        <div>{{ getDictLabel('loan_order_state', row.state) }}</div>
      </template>
    </Descriptions>
  </ContentWrap>

  <ContentWrap title="费用数据">
    <div class="mb-10px"> </div>
    <Table :columns="d_columnss" :data="data.detailed" :selection="false" :border="true">
      <template #type="{ row }">
        <div>{{ getDictLabel('zcfy_type_sub', row.type) }}</div>
      </template>

      <template #amount="{ row }">
        <div>{{ row.amount }}</div>
      </template>

      <template #note="{ row }">
        <div>{{ row.note }}</div>
      </template>

      <!-- <template #state="{ row }">
        <div>{{ row.state }}</div>
      </template>

      <template #medical_user_id="{ row }">
        <div>{{ row.medical_user_id }}</div>
      </template> -->
    </Table>
  </ContentWrap>

  <ContentWrap title="明细数据">
    <div class="mb-10px"> </div>
    <Table :columns="d_columns" :data="detailedData" :selection="false" :border="true">
      <template #earcon_id="{ row }">
        <div>{{ row.earcon_id }}</div>
      </template>

      <template #medical_date="{ row }">
        <div>{{ row.medical_date }}</div>
      </template>

      <template #symptom="{ row }">
        <div>{{ row.symptom }}</div>
      </template>

      <template #state="{ row }">
        <div>{{ row.state }}</div>
      </template>

      <template #medical_user_id="{ row }">
        <div>{{ row.medical_user_id }}</div>
      </template>
    </Table>
  </ContentWrap>
</template>
